package com.victor9ac.tfm.twitter.api;

import java.util.ArrayList;
import java.util.List;




import twitter4j.*;
import twitter4j.conf.ConfigurationBuilder;

public class TwitterService {

	
	
	/**
	 * singleton
	 */
	private static TwitterService twitterService;

	public static  TwitterService getInstance(){
		return twitterService == null ? twitterService = new TwitterService() : twitterService;
	}
	
	/**
	 * my Twitter credentials
	 */
	private static final String CONSUMER_SECRET = "6Sz1YjAPd3CkUDuHIG2SRHXOsLB77CmcbcZGyRJGPy0mcWLZzw";
	private static final String CONSUMER_KEY = "ZoM3RGH7x48G3KOeXsK3OTudF";
	private static final String ACCESS_TOKEN = "192599808-HPgopXHLy2TPYPC7rS83GfMlZIcYIW2ujDo5NqeG";
	private static final String ACCESS_TOKEN_SECRET = "UWq35LoK3MgaU0ziujklKkJZesIqjHHM7PIGO4HJMcgFu";

	
	/**
	 * Collaborators
	 */
	Twitter twitter;

	private TwitterService() {
		twitter = buildTwitterInstance();

	}

	private Twitter buildTwitterInstance() {

		ConfigurationBuilder cb = new ConfigurationBuilder();
		cb.setDebugEnabled(true).setOAuthConsumerKey(CONSUMER_KEY)
				.setOAuthConsumerSecret(CONSUMER_SECRET)
				.setOAuthAccessToken(ACCESS_TOKEN)
				.setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET);

		TwitterFactory tf = new TwitterFactory(cb.build());
		Twitter twitterInstance = tf.getInstance();

		return twitterInstance;
	}


	public List<Status> getTweets(String tweetSearch) {
		List<Status> tweetList = new ArrayList<Status>();
		try {
			tweetList = buildTweetQuery(tweetSearch);
		} catch (TwitterException e) {
			e.printStackTrace();
		}
		return tweetList;
	}

	private List<Status> buildTweetQuery(String tweetSearch)
			throws TwitterException {
		Query query = new Query(tweetSearch);
		QueryResult result = twitter.search(query);

		List<Status> tweetList = result.getTweets();
		return tweetList;
	}

}
